package com.quantstack.llcommonlibrary.base;

import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.databinding.DataBindingUtil;
import androidx.databinding.ViewDataBinding;
import androidx.fragment.app.Fragment;
import androidx.lifecycle.ViewModelProvider;

import com.hjq.toast.ToastUtils;
import com.quantstack.llcommonlibrary.dialog.LoadingDialog;

import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;

/**
 * @author wu_ming_zhi_bei
 * @date 2021/1/26 14:13
 * @Notes
 */
public abstract class BaseFragment <VM extends BaseViewModel, VDB extends ViewDataBinding> extends Fragment implements BaseView{

    protected VM mViewModel;
    protected VDB mViewDataBind;
    private LoadingDialog loadingDialog;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        mViewDataBind = DataBindingUtil.inflate(inflater,getLayoutId(),container,false);
        mViewDataBind.setLifecycleOwner(this);
        //获得泛型参数的实际类型
        if (mViewModel == null) {
            Class modelClass;
            Type type = getClass().getGenericSuperclass();
            if (type instanceof ParameterizedType) {
                modelClass = (Class) ((ParameterizedType) type).getActualTypeArguments()[0];
            } else {
                //如果没有指定泛型参数，则默认使用BaseViewModel
                modelClass = BaseViewModel.class;
            }
            //和activity共享数据
            mViewModel = (VM) new ViewModelProvider(getActivity()).get(modelClass);
        }
        return mViewDataBind.getRoot();
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        afterCreate();
    }

    public abstract int getLayoutId();
    public abstract void afterCreate();


    /**
     * 直接跳转Activity
     * @param cls
     */
    public void go(Class cls){
        Intent intent = new Intent(getActivity(),cls);
        getActivity().startActivity(intent);
    }

    /**
     * 携带数据跳转Activity
     * @param cls
     */
    public void go(Class cls,Bundle bundle){
        Intent intent = new Intent(getActivity(),cls);
        getActivity().startActivity(intent,bundle);
    }

    /**
     * 携带返回值数据跳转Activity
     * @param cls
     */
    public void go(Class cls, int resultCode){
        Intent intent = new Intent(getActivity(),cls);
        getActivity().startActivityForResult(intent,resultCode);
    }

    /**
     * 携带返回值数据跳转Activity
     * @param cls
     */
    public void go(Class cls,Bundle bundle,int resultCode){
        Intent intent = new Intent(getActivity(),cls);
        getActivity().startActivityForResult(intent,resultCode,bundle);
    }


    /**
     * 封装toast方法（自行去实现）
     *
     * @param str
     */
    public void showToast(String str) {
        ToastUtils.show(str);
    }

    public void showLongToast(String str) {
        ToastUtils.show(str);
    }

    /**
     * 消失  黑框...
     */
    @Override
    public void dissMissDialog() {
        if (loadingDialog != null) {
            loadingDialog.dismiss();
        }
    }

    public void showLoadingDialog(){
        showLoadingDialog("loading...");
    }
    @Override
    public void showLoadingDialog(String msg) {
        if (loadingDialog == null) {
            loadingDialog = new LoadingDialog(getActivity());
        }
        loadingDialog.setMessage(msg);
        if (!loadingDialog.isShowing()) {
            loadingDialog.show();
        }
    }

}
